//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AnalyticCapFloorEngine.h"
using namespace Cephei::QL::Pricingengines::Capfloor;
#include <gen/QL/Models/AffineModel.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (Cephei::QL::Models::IAffineModel^ model, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ termStructure) : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
    CAffineModel^ _Cmodel;
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phAnalyticCapFloorEngine = NULL;
#endif
        _Cmodel = safe_cast<CAffineModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::AffineModel>& _model = static_cast<boost::shared_ptr<QuantLib::AffineModel>&> (_Cmodel->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (termStructure))
        {
            _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure->Value);
            _CtermStructure->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _termStructure = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (termStructure) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppAnalyticCapFloorEngine = new boost::shared_ptr<QuantLib::AnalyticCapFloorEngine> (new QuantLib::AnalyticCapFloorEngine ( _model,  _termStructure ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (boost::shared_ptr<QuantLib::AnalyticCapFloorEngine>& childNative, Object^ owner) : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticCapFloorEngine = NULL;
#endif
	_ppAnalyticCapFloorEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
}
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (QuantLib::AnalyticCapFloorEngine& childNative, Object^ owner) : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticCapFloorEngine = NULL;
#endif
	_ppAnalyticCapFloorEngine = new boost::shared_ptr<QuantLib::AnalyticCapFloorEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
    _AnalyticCapFloorEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (CAnalyticCapFloorEngine^ copy) : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticCapFloorEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticCapFloorEngine = new boost::shared_ptr<QuantLib::AnalyticCapFloorEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
    }
}
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (PLATFORM::Type^ t) : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticCapFloorEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticCapFloorEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (QuantLib::Handle<QuantLib::AnalyticCapFloorEngine>& childNative, Object^ owner)  : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
	_phAnalyticCapFloorEngine = &childNative;
	_ppAnalyticCapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticCapFloorEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
    _AnalyticCapFloorEngineOwner = owner;
}
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (QuantLib::Handle<QuantLib::AnalyticCapFloorEngine> childNative)  : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
	_phAnalyticCapFloorEngine = &childNative;
	_ppAnalyticCapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticCapFloorEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::CAnalyticCapFloorEngine (QuantLib::AnalyticCapFloorEngine childNative)  : CPricingEngine(CAnalyticCapFloorEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticCapFloorEngine = NULL;
#endif
	_ppAnalyticCapFloorEngine = new boost::shared_ptr<QuantLib::AnalyticCapFloorEngine> (new QuantLib::AnalyticCapFloorEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticCapFloorEngine));
}
#endif

Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::~CAnalyticCapFloorEngine ()
{
    if (_ppAnalyticCapFloorEngine != NULL)
    {
	    delete _ppAnalyticCapFloorEngine;
        _ppAnalyticCapFloorEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::!CAnalyticCapFloorEngine ()
{
    if (_ppAnalyticCapFloorEngine != NULL)
    {
	    delete _ppAnalyticCapFloorEngine;
    }
}
QuantLib::AnalyticCapFloorEngine& Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::GetReference ()
{
    if (_ppAnalyticCapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppAnalyticCapFloorEngine;
}
boost::shared_ptr<QuantLib::AnalyticCapFloorEngine>& Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::GetShared ()
{
    if (_ppAnalyticCapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppAnalyticCapFloorEngine;
}
QuantLib::AnalyticCapFloorEngine* Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::GetPointer ()
{
    if (_ppAnalyticCapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppAnalyticCapFloorEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticCapFloorEngine>& Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::GetHandle ()
{
	if (_phAnalyticCapFloorEngine == NULL)
	{
		_phAnalyticCapFloorEngine = new Handle<QuantLib::AnalyticCapFloorEngine> (*_ppAnalyticCapFloorEngine);
	}
	return *_phAnalyticCapFloorEngine;
}
#endif
bool Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::HasNative () 
{
	return (_ppAnalyticCapFloorEngine != NULL);
}

Cephei::QL::Pricingengines::Capfloor::IAnalyticCapFloorEngine^ Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticCapFloorEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Capfloor::IAnalyticCapFloorEngine^ Cephei::QL::Pricingengines::Capfloor::CAnalyticCapFloorEngine_Factory::Create (Cephei::QL::Models::IAffineModel^ model, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ termStructure)
{
    return REFNEW CAnalyticCapFloorEngine ( model,  termStructure);
}
